package com.amos.teaching_aids.model;

import com.amos.teaching_aids.net.STError;
import com.amos.teaching_aids.net.STHttpCallback;
import com.amos.teaching_aids.net.STJsonQuery;
import com.amos.teaching_aids.net.STNetTask;
import com.amos.teaching_aids.net.STRequest;
import com.amos.teaching_aids.utils.SharedPreferencesHelper;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/10/8 0008.
 */

public class UserModel implements IUserModel {

    @Override
    public void login(String url, String id, String pass, String identify, final onLoginListener listener,
                      STNetTask.BeforeQueryHook beforeQueryHook, STNetTask.AfterQueryHook afterQueryHook) {
        STHttpCallback callback = new STHttpCallback() {
            @Override
            public void onSuccess(String response) {
                listener.onLoginSuccess();

            }

            @Override
            public void onFailed(STError error) {
                listener.onLoginFailure(error.getReason());

            }
        };
        SharedPreferencesHelper.getInstance().saveData("id", id);
        SharedPreferencesHelper.getInstance().saveData("identify", identify);
        Map<String, Object> params = new HashMap();
        params.put(identify + "Id", id);
        params.put(identify + "Password", pass);
        STRequest request = new STRequest(url, params, null);
        STJsonQuery jsonQuery = new STJsonQuery();
        jsonQuery.setBeforeQueryHook(beforeQueryHook);
        jsonQuery.setAfterQueryHook(afterQueryHook);
        jsonQuery.post(request, callback);

    }

    @Override
    public void register(String url, String id, String pass, String name, String major, String email,
                         String majorClass, String identify, final onRegListener listener,
                         STNetTask.BeforeQueryHook beforeQueryHook, STNetTask.AfterQueryHook afterQueryHook) {
        STHttpCallback callback = new STHttpCallback() {
            @Override
            public void onSuccess(String response) {
                listener.onRegSuccess();
            }

            @Override
            public void onFailed(STError error) {
                listener.onRegFailure(error.getReason());
            }
        };
        Map<String, Object> params = new HashMap();
        params.put(identify + "Id", id);
        params.put(identify + "Password", pass);
        params.put(identify + "Name", name);
        params.put(identify + "Major", major);
        params.put(identify + "Email", email);
        if (identify.equals("student")){
            params.put(identify + "Class", majorClass);
        }
        SharedPreferencesHelper.getInstance().saveObject(id, params);
        STRequest request = new STRequest(url, params, null);
        STJsonQuery jsonQuery = new STJsonQuery();
        jsonQuery.setBeforeQueryHook(beforeQueryHook);
        jsonQuery.setAfterQueryHook(afterQueryHook);
        jsonQuery.post(request, callback);
    }

    @Override
    public void logout(String url, String id, String identify, final onLogoutListener listener,
                       STNetTask.BeforeQueryHook beforeQueryHook, STNetTask.AfterQueryHook afterQueryHook) {
        STHttpCallback callback = new STHttpCallback() {
            @Override
            public void onSuccess(String response) {
                listener.onLogoutSuccess();
            }

            @Override
            public void onFailed(STError error) {
                listener.onLogoutFailure(error.getReason());

            }
        };
        Map<String, Object> params = new HashMap();
        params.put(identify + "Id", id);
        STRequest request = new STRequest(url, params, null);
        STJsonQuery jsonQuery = new STJsonQuery();
        jsonQuery.setBeforeQueryHook(beforeQueryHook);
        jsonQuery.setAfterQueryHook(afterQueryHook);
        jsonQuery.post(request, callback);

    }

    @Override
    public String getId() {
        return SharedPreferencesHelper.getInstance().getData("id", "").toString();
    }

    @Override
    public String getIdentify() {
        return SharedPreferencesHelper.getInstance().getData("identify", "").toString();
    }

    @Override
    public Map<String, String> getUserInro() {
        SharedPreferencesHelper.getInstance().saveData("id", "123");
        SharedPreferencesHelper.getInstance().saveData("identify", "teacher");

        Map<String, String> map = new HashMap<>();
        map.put("teacherId", "123");
        map.put("teacherName", "张三");
        map.put("teacherMajor", "aa");
        map.put("teacherEmail", "5220@qq.com");
        SharedPreferencesHelper.getInstance().saveObject("123", map);
        Map<String, String> params =  new HashMap<>();
        params = (Map<String, String>)SharedPreferencesHelper.getInstance().getObject(getId(), "");
        return params;
    }

    @Override
    public void saveRadioCheck(String checked){
        SharedPreferencesHelper.getInstance().saveData("radiocheck", checked);
    }

    @Override
    public String getRadioCheck(){
        return SharedPreferencesHelper.getInstance().getData("radiocheck", "").toString();
    }

    public interface onLoginListener{
        void onLoginSuccess();
        void onLoginFailure(String errorReason);
    }
    public interface onRegListener{
        void onRegSuccess();
        void onRegFailure(String errorReason);
    }
    public interface onLogoutListener{
        void onLogoutSuccess();
        void onLogoutFailure(String errReason);
    }
}
